/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.nacos.naming.push.v2.task;

import com.alibaba.nacos.common.task.AbstractDelayTask;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.misc.Loggers;

import java.util.HashSet;
import java.util.Set;

/**
 * Nacos naming push delay task.
 *
 * @author xiweng.yy
 */
public class PushDelayTask extends AbstractDelayTask {

    /**
     * 目标 service
     */
    private final Service service;

    private boolean pushToAll;

    /**
     * 需要推送的 clientId
     */
    private Set<String> targetClients;

    public PushDelayTask(Service service, long delay) {
        this.service = service;
        pushToAll = true;
        targetClients = null;
        setTaskInterval(delay);
        setLastProcessTime(System.currentTimeMillis());
    }

    /**
     * 延时任务 task
     *
     * @param service      目标 service
     * @param delay        延迟时间
     * @param targetClient 需要推送的 clientId
     */
    public PushDelayTask(Service service, long delay, String targetClient) {
        this.service = service;
        this.pushToAll = false;
        this.targetClients = new HashSet<>(1);
        this.targetClients.add(targetClient);
        setTaskInterval(delay);

        // 设置上一次处理时间，用来判断是否过期
        setLastProcessTime(System.currentTimeMillis());
    }


    /**
     * 每一个延时任务都会有 merge 方法, 用来合并相同的 task, 这样可以更高效的处理任务
     * 比如因为客户端重试，发起了两个一样的 task，经过 merge 之后，处理一个就行。
     *
     * @param task task
     */
    @Override
    public void merge(AbstractDelayTask task) {
        if (! (task instanceof PushDelayTask)) {
            return;
        }
        PushDelayTask oldTask = (PushDelayTask) task;
        if (isPushToAll() || oldTask.isPushToAll()) {
            pushToAll = true;
            targetClients = null;
        } else {
            targetClients.addAll(oldTask.getTargetClients());
        }

        // 设置上一次处理时间，用来判断是否过期
        setLastProcessTime(Math.min(getLastProcessTime(), task.getLastProcessTime()));


        Loggers.PUSH.info("[PUSH] Task merge for {}", service);
    }

    public Service getService() {
        return service;
    }

    public boolean isPushToAll() {
        return pushToAll;
    }

    public Set<String> getTargetClients() {
        return targetClients;
    }
}
